<style scoped>
.mask{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.3;
    z-index: 9;
}
._ReviSion{
    position: fixed;
    top: 15%;
    left: 50%;
    transform: translateX(-50%);
    width: 1195px;
    height: 630px;
    z-index: 99;
    background-color: white;
    overflow: auto;
}
.RS_top{
    height: 40px;
    background: #f8f8f8;
    display: flex;
    justify-content: space-between;
    padding: 10px 15px;
}
.RS_top p{
    font-weight: 700;
}
.RS_middle{
    width: 1193px;
    /* border: 1px solid black; */
    margin: 10px auto;
}
.button_box{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0 15px 15px;
    display: flex;
}
.inp_box2{
    background: #f8f8f8;
    width: 100%;
    height: 60px;
    padding: 15px 0;
    margin: 15px 0;
    display: flex;
}
.inp_box{
  display: flex;
  margin: 15px 0 10px 0;
  position: relative;
  left: -28px;
  width: 1220px;
}
.inp_box p , .inp_box2 p{
  display: inline-block;
  width: 80px;
  text-align: right;
  padding: 0 5px 0 10px;
  padding-right: 5px;
  line-height: 30px;
}

.ReviSion input:focus{
    outline: none;
    border: 1px solid #2a8ffc;
  }
  .inp{
    border: 1px solid #dbdbdb;
    border-radius: 3px;
    height: 30px;
    padding: 0 15px;
  }
  .inp2{
    width: 160px;
  }
  .inp3{
    width: 60px;
    margin-right: 10px;
  }
  .inp4{
      width: 400px;
  }
  .inp5{
    width: 121px;
    margin-right: 10px;
  }
  .inp6{
    width: 160px;
  }
  .RS_button{
      margin-left: 1070px;
  }
  .mg{
    margin-left: 32px;
  }
</style>

<style>
.ReviSion .inp_box .el-input__inner {
    width: 160px;
}
.ReviSion .el-input .el-input__inner {
    height: 30px!important;
}
.ReviSion .inp_box .sty .el-input__inner {
    width: 90px;
}
.ReviSion .inp_box .sty2 .el-input__inner {
    width: 160px;
}
.ReviSion .inp_box .sty3 .el-input__inner {
    width: 160px;
}
.ReviSion .RS_middle .inp_box2 .el-input__inner {
    width: 160px;
}
.ReviSion .el-checkbox.is-bordered{
    padding: 4px 20px 9px 8px;
    height: 30px;
    width: 32px;
}

</style>

<template>
  <div class="ReviSion">
    <div class="mask"></div>
    <div class="_ReviSion">
      <div class="RS_top">
        <p>改单(客户进货)</p>
        <i @click="closeReviSion" class="el-icon-close"></i>
      </div>
      <div class="RS_middle">
        <div class="button_box">
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button icon="el-icon-edit-outline" @click="modification(0)">改单</el-button></div>
        <div class="inp_box">
          <p>单号</p>
          <input class="inp inp2" type="text" v-model="receiptsNumber" disabled>
          <p>进货日期</p>
          <el-date-picker class="inp2" v-model="inputDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" disabled></el-date-picker>
          <p>货号</p>
          <div class="sty2">
            <el-input v-model="kindsNumber" disabled></el-input>
          </div>
          <p>客户编号</p>
          <input class="inp inp3" type="text" v-model="customerNo" @blur="customerNoBlur" :disabled="lockNo">
          <div class="sty">
            <el-select v-model="customerName" @change="customerChange" :disabled="lockNo">
              <el-option v-for="item in customerL" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </div>
          <p>仓库</p>
          <input disabled class="inp inp3" type="text" v-model="warehouseNo" @blur="warehouseNoBlur">
          <div class="sty">
            <el-select v-model="warehouseName" @change="warehouseChange" disabled>
              <el-option v-for="item in warehouseL" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </div>
        </div>
        <div class="inp_box">
          <p>折数</p>
          <div class="sty2">
            <el-input v-model="discount" @blur="discountBlur" :disabled="checked || changeNo"></el-input>
          </div>
          <p>客进序号</p>
          <div class="sty2">
            <el-input v-model="inputSequence" disabled></el-input>
          </div>
          <p>实收单价</p>
          <input class="inp inp5" type="text" v-model="actualPrice" :disabled="!checked || changeNo" @blur="actualPriceBlur">
          <el-checkbox v-model="checked" border :disabled="changeNo"></el-checkbox>
          <p>配货方式</p>
          <div class="sty3">
            <el-select v-model="cooperateWay" :disabled="changeNo">
              <el-option v-for="item in cooperateWayList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </div>
          <p>配货序号</p>
          <input class="inp inp6" type="text" v-model="cooperateSequence" disabled>
        </div>

        <div class="inp_box">
          <p>数量</p>
          <input class="inp inp6" type="text" width="160px" v-model="sum" :disabled="isNewBuild || changeNo" @blur="sumBlur">
          <p>备注</p>
          <input class="inp inp4" type="text" v-model="remark" :disabled="changeNo">
        </div>

        <div class="inp_box2">
          <p style="width:55px">货号</p>
          <div class="sty2">
            <el-input v-model="searchKindsNumber"></el-input>
          </div>
          <p>客进序号</p>
          <div class="sty2">
            <el-input v-model="searchInputSequence"></el-input>
          </div>
        </div>
        <el-table height="250" :data="tableData" 
          highlight-current-row 
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" 
          :cell-style="{padding:0,height:'36px'}" 
          style="width: 98%;margin: 0 auto" 
          @row-click="rowClick">
          <el-table-column type="index" label="序号" width="50"></el-table-column>
          <el-table-column prop="sequence" label="客进序号" width="120"></el-table-column>
          <el-table-column prop="receiptsNumber" label="单号" width="100"></el-table-column>
          <el-table-column prop="warehouseNo" label="仓库编号" width="80"></el-table-column>
          <el-table-column prop="kindsNumber" label="货号" min-width="160"></el-table-column>
          <el-table-column prop="colorNameNo" label="色码" width="80"></el-table-column>
          <el-table-column prop="sizeNo" label="尺码编号" width="80"></el-table-column>
          <el-table-column prop="sum" label="数量" width="80"></el-table-column>
          <el-table-column prop="sellingPrice" label="售价" width="80"></el-table-column>
          <el-table-column prop="discount" label="折数" width="50">
            <template slot-scope="scope">
              <el-input size="small" 
                class="minipadInp"
                v-model="scope.row.discount" 
                @blur="itemDiscount(scope.row)">
              </el-input>
            </template>
          </el-table-column>
          <el-table-column prop="onePrice" label="单价" width="100"></el-table-column>
          <el-table-column prop="timeShort" label="进货日期" width="100"></el-table-column>
          <el-table-column prop="cooperateWay" label="配货方式" width="80"></el-table-column>
        </el-table>
      </div>
      <!-- <el-button @click="closeReviSion" class="RS_button">取消</el-button>
      <el-button type="primary" @click="modification(1)">保存</el-button> -->
    </div>
  </div>
</template>

<script>
import { updateReceipt, getInfoByInputSequence, affirmUpdateReceipt, updateByAffirm } from '../../../../network/customerInputTotal'

import today from '../../../../components/mixins/today'

export default {
  mixins: [today],
  props: {
    selectInputSequence: {
      type: String,
      default: ''
    },
    customerL: {
      type: Array,
      default: []
    },
    warehouseL: {
      type: Array,
      default: []
    },
    flag: {
      type: Boolean,
      default: false
    },
    detailRow: {
      type: Object,
      default: {}
    },
    Str: {
      type: String,
      default: ''
    }
  },
  data(){
    return{
      updateData: {},

      inputDate: '',
      customerNo: '',
      customerName: '',
      receiptsNumber: '',
      inputSequence: '',
      kindsNumber: '',
      warehouseNo: '',
      warehouseName: '',
      discount: '',
      cooperateWay: '',
      cooperateWayList: [
        {value:'配货',label:'配货'},
        {value:'订货',label:'订货'},
        {value:'订断',label:'订断'},
        {value:'追断',label:'追断'}
      ],
      checked: false,
      changeNo: false,
      lockNo: false,
      actualPrice: '',
      cooperateSequence: '',
      sum: '',
      isNewBuild: false,
      isAffirm: false,
      remark: '',

      searchKindsNumber: '',
      searchInputSequence: '',

      tableData: []
    }
  },
  watch: {
    flag(a,b){
      if (a) {
        if (JSON.stringify(this.detailRow) == "{}") {
          this.search()
        } else {
          this.searchKindsNumber = this.detailRow.kindsNumber
          this.tableData = [this.detailRow]
          this.fillBlanks(this.detailRow)
          this.rowClick(this.detailRow)
        }
      }
    },
    selectInputSequence(a,b){
      if (a) {
        this.searchInputSequence = a
        this.inputSequence = a
      }
    },
    checked(a,b){
      if (a && !this.lockNo) {
        this.lockNo = true
      }
      this.updateData.isOfficialReceipts = a ? 1 : 0
    },
    receiptsNumber(a,b){
      this.receiptsNumber = a.toUpperCase().trim()
      if (this.tableData.length && a != this.tableData[0].receiptsNumber && !this.lockNo) {
        this.lockNo = true
      }
      this.tableData.forEach(e => {
        e.receiptsNumber = a
      })
      this.$forceUpdate()
    },
    // inputDate(a,b){
    //   if (this.tableData.length && a != this.tableData[0].purchaseTime && !this.lockNo) {
    //     this.lockNo = true
    //   }
    //   this.tableData.forEach(e => {
    //     e.purchaseTime = a
    //     e.timeShort = a.slice(0,10)
    //   })
    // },
    cooperateWay(a,b){
      if (this.tableData.length && a != this.tableData[0].cooperateWay && !this.lockNo) {
        this.lockNo = true
      }
      this.tableData.forEach(e => {
        e.cooperateWay = a
      })
    },
    remark(a,b){
      if (this.tableData.length && a != this.tableData[0].remark && !this.lockNo) {
        this.lockNo = true
      }
      this.tableData.forEach(e => {
        e.remark = a
      })
    },
    kindsNumber(a,b){
      this.kindsNumber = a.toUpperCase().trim()
    },
    customerNo(a,b){
      this.customerNo = a.toUpperCase().trim()
    },
    warehouseNo(a,b){
      this.warehouseNo = a.toUpperCase().trim()
    },
    searchKindsNumber(a,b){
      this.searchKindsNumber = a.toUpperCase().trim()
    },
    searchInputSequence(a,b){
      this.searchInputSequence = a.toUpperCase().trim()
    },
  },
  methods:{
    clearData(){
      this.inputDate = ''
      this.customerNo = ''
      this.customerName = ''
      this.receiptsNumber = ''
      this.inputSequence = ''
      this.kindsNumber = ''
      this.warehouseNo = ''
      this.warehouseName = ''
      this.discount = ''
      this.cooperateWay = ''
      this.checked = false
      this.actualPrice = ''
      this.cooperateSequence = ''
      this.sum = ''
      this.remark = ''
      this.searchKindsNumber = ''
      this.tableData = []
      this.lockNo = false
      this.changeNo = false
    },
    closeReviSion(){
      this.$emit('closeReviSion')
      this.clearData()
    },
    // 客户编号失去焦点
    customerNoBlur(){
      if (this.customerNo) {
        for (const i of this.customerL) {
          if (this.customerNo == i.value) {
            this.customerName = i.label
            if (this.tableData.length && this.customerNo != this.tableData[0].no) {
              this.changeNo = true
              this.tableData.forEach(e => {
                e.no = this.customerNo
                e.name = i.label
              })
            }
            return
          }
        }
        this.$message.error('无此客户编号')
        this.customerNo = this.tableData[0].no
        this.customerNoBlur()
      } else {
        this.customerName = this.tableData[0].no
      }
    },
    // 仓库编号失去焦点
    warehouseNoBlur(){
      if (this.warehouseNo != '') {
        for (const i of this.warehouseL) {
          if (this.warehouseNo == i.value) {
            this.warehouseName = i.label
            if (this.tableData.length && this.warehouseNo != this.tableData[0].warehouseNo) {
              if(!this.lockNo) this.lockNo = true
              this.tableData.forEach(e => {
                e.warehouseNo = this.warehouseNo
              })
            }
            return
          }
        }
        this.$message.error('无此仓库编号')
        this.warehouseNo = this.tableData[0].warehouseNo
        this.warehouseNoBlur()
      } else {
        this.warehouseName = this.tableData[0].warehouseNo
      }
    },

    discountBlur(){
      let a = this.discount
      let r = /^\d(\.\d)?$|^10$/
      if (!r.test(a)) {
        this.$alert('折数请输入10以内数字，小数不得超过一位', '', {
          confirmButtonText: '确定'
        })
        this.discount = this.updateData.discount
        return
      }
      if (this.tableData.length && a != this.tableData[0].discount && !this.lockNo) {
        this.lockNo = true
      }
      this.tableData.forEach(e => {
        e.discount = a
        e.onePrice = (e.sellingPrice * a / 10).toFixed(2)
        e.money = (e.onePrice * e.sum).toFixed(2)
        e.updateMoney = (e.money - e.initialMoney).toFixed(2)
      })
      // this.updateData.discount = a
      // this.updateData.onePrice = (this.updateData.sellingPrice * a / 10).toFixed(2)
      // this.updateData.money = (this.updateData.onePrice * this.updateData.sum).toFixed(2)
      // this.updateData.updateMoney = (this.updateData.money - this.updateData.initialMoney).toFixed(2)
    },
    actualPriceBlur(){
      let a = this.actualPrice
      let r = /^[1-9]\d*(.[0-9]{1,2})?$/
      if (!r.test(a)) {
        this.$alert('实收单价请输入数字，小数不得超过两位', '', {
          confirmButtonText: '确定'
        })
        this.actualPrice = this.updateData.onePrice
        return
      }
      if (this.tableData.length && a != this.tableData[0].onePrice && !this.lockNo) {
        this.lockNo = true
      }
      this.updateData.onePrice = a
      this.discount = (a / this.updateData.sellingPrice * 10).toFixed(1)
      this.updateData.discount = this.discount
      this.updateData.money = (this.updateData.onePrice * this.updateData.sum).toFixed(2)
      this.updateData.updateMoney = (this.updateData.money - this.updateData.initialMoney).toFixed(2)
    },
    sumBlur(){
      let a = this.sum
      let r = /^(0|[1-9]\d*)$/
      if (!r.test(a)) {
        this.$alert('数量请输入整数', '', {
          confirmButtonText: '确定'
        })
        this.sum = this.updateData.sum
        return
      }
      if (this.tableData.length && a != this.tableData[0].sum && !this.lockNo) {
        this.lockNo = true
      }
      this.updateData.sum = a
      this.updateData.updateNumber = a - this.updateData.initialSum
      this.updateData.money = (this.updateData.onePrice * this.updateData.sum).toFixed(2)
      this.updateData.updateMoney = (this.updateData.money - this.updateData.initialMoney).toFixed(2)
    },

    warehouseChange(e){
      this.warehouseNo = e
      if (this.tableData.length && this.warehouseNo != this.tableData[0].warehouseNo && !this.lockNo) {
        this.lockNo = true
        this.tableData.forEach(e => {
          e.warehouseNo = this.warehouseNo
        })
      }
    },
    customerChange(e){
      this.customerNo = e
      for(let b of this.customerL){
        if(e == b.value){
          this.discount = b.discount
        }
      }
      if (this.tableData.length && this.customerNo != this.tableData[0].no) {
        this.changeNo = true
        for (const i of this.customerL) {
          if (i.value == this.customerNo) {
            this.tableData.forEach(e => {
              e.no = this.customerNo
              e.name = i.label
            })
            return
          }
        }
      }
    },

    itemDiscount(row){
      if (!this.lockNo) this.lockNo = true
      row.onePrice = (row.sellingPrice * row.discount / 10).toFixed(2)
      row.money = (row.onePrice * row.sum).toFixed(2)
      row.updateMoney = (row.money - row.initialMoney).toFixed(2)
      // console.log(row)
    },

    // isNewBuild是0不能改数量，别的都可以改
    fillBlanks(row){
      this.updateData = row
      
      this.isNewBuild = (row.isNewBuild ? false : true)
      this.receiptsNumber = row.receiptsNumber
      this.kindsNumber = row.kindsNumber
      this.inputDate = row.purchaseTime
      this.customerNo = row.no
      this.customerName = row.name
      this.warehouseNo = row.warehouseNo
      this.inputSequence = row.sequence
      this.cooperateWay = row.cooperateWay
      this.cooperateSequence = row.cooperateSequence
      this.remark = row.remark

      this.warehouseNoBlur()
    },
    rowClick(row){
      // console.log(row)
      this.sum = row.sum
      this.discount = row.discount
      this.actualPrice = row.onePrice
      this.updateData = row
      // this.checked2 = row.isReturn ? true : false
      this.checked3 = row.isOfficialReceipts ? true : false
    },

    // 查询
    search(){
      if (!this.searchInputSequence && !this.searchKindsNumber) {
        this.$alert('请输入查询条件', '', {
          confirmButtonText: '确定'
        })
        return
      }
      this.tableData = []
      if (this.searchKindsNumber) {
        updateReceipt({
          inputSequence: this.searchInputSequence,
          kindsNumber: this.searchKindsNumber
        }).then(res => {
          if (res.data.data.length) {
            this.tableData = res.data.data
            this.tableData.forEach(e => {
              e.colorNameNo = e.colorName + ' ' + e.size
              e.timeShort = e.purchaseTime.slice(0,10)
              e.oldNo = e.no
              e.initialSum = e.sum
              e.initialMoney = e.money
              // 是否实价 0否1是
              e.isOfficialReceipts = 0
              // e.isReturn = 0
            })
            this.fillBlanks(this.tableData[0])
            this.isAffirm = this.tableData[0].affirm
            if (this.lockNo) this.lockNo = false
            if (this.changeNo) this.changeNo = false
          }
        })
      } else {
        getInfoByInputSequence({inputSequence: this.searchInputSequence}).then(res => {
          if (res.data.data.length) {
            this.tableData = res.data.data
            this.tableData.forEach(e => {
              e.colorNameNo = e.colorName + ' ' + e.size
              e.timeShort = e.purchaseTime.slice(0,10)
              e.oldNo = e.no
              e.initialSum = e.sum
              e.initialMoney = e.money
              // 是否实价 0否1是
              e.isOfficialReceipts = 0
            })
            this.fillBlanks(this.tableData[0])
            this.isAffirm = this.tableData[0].affirm
            if (this.lockNo) this.lockNo = false
            if (this.changeNo) this.changeNo = false
          }
        })
      }
    },
    
    modification(f){
      this.tableData.forEach(e=>{
        e.flagToken = this.Str
      })
      if (this.isAffirm) {
        console.log(this.tableData)
        updateByAffirm(JSON.stringify(this.tableData)).then(res => {
          if (res.data.code == 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.searchInputSequence = this.inputSequence
            this.search()
            this.$emit('modifySuc')
          } else {
            this.$message.error('修改失败')
          }
        })
      } else {
        console.log(this.tableData)
        affirmUpdateReceipt(JSON.stringify(this.tableData)).then(res => {
          if (res.data.code == 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.searchInputSequence = this.inputSequence
            this.search()
            this.$emit('modifySuc')
          } else {
            this.$message.error('修改失败')
          }
        })
      }

      if (f) {
        this.closeReviSion()
      }
    }
  }
}
</script>